package company.permissionsystem.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import company.permissionsystem.security.roleassignment.PermissionAssignment;
import company.permissionsystem.model.entity.SysUser;
import company.permissionsystem.security.roleassignment.factory.RoleFactory;
import company.permissionsystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
/**
 *  首页及欢迎页面
 *  员工页面
 * */
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/")
    public String index(@AuthenticationPrincipal UserDetails userDetails, Model model) {
        Collection<? extends GrantedAuthority> authorities = userDetails.getAuthorities();
        String[] list = authorities.toString().replace(" ", "").replace("[", "").replace("]", "").split(",");
//        PermissionAssignment power = RoleEnum.valueOf( list[list.length - 1] ).op();
        PermissionAssignment power = RoleFactory.get( list[list.length - 1] ).op();
        model.addAttribute("role", power);
        return "index";
    }

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    /**
     *  欢迎页面加载
     * */
    @GetMapping("/welcome")
    public String welcome(@AuthenticationPrincipal UserDetails userDetails,Model model) {
        model.addAttribute("user", userDetails.getUsername());
        return "welcome";
    }

    @GetMapping("/swagger-ui")
    @PreAuthorize("hasAuthority('admin:swagger')")
    public String SwaggerIndex () {
        return "swagger-ui";
    }

    @GetMapping("/druid/index")
    @PreAuthorize("hasAuthority('admin:druid')")
    public String DruidIndex(){
        return "druid/index";
    }

    /**
     *  进入主页加载用户
     * */
    @GetMapping("/employee")
    public String addUsers(@RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum, Model model) {
        PageHelper.startPage(pageNum, 8);
        List<SysUser> sysUsers = userService.findAllUser();
        PageInfo<SysUser> lists = new PageInfo<>(sysUsers);
        model.addAttribute("users", lists);
        return "employee/employee-list";
    }

      /**
       *    使用json返回带有分页的结果集
       * */
//    @GetMapping("/employee")
//    @ResponseBody
//    public Results addUsers(@RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
//        PageHelper.startPage(pageNum, 8);
//        List<SysUser> sysUsers = userService.findAllUser();
//        PageInfo<SysUser> lists = new PageInfo<>(sysUsers);
//        System.out.println();
//        return Results.success("success", lists.getSize(), lists.getList());
//    }

    /**
     *  更改用户状态
     * */
    @PreAuthorize("hasAuthority('user:edit')")
    @GetMapping("/employee/{id}/change")
    public String changeInStatus(@PathVariable Long id) {
        userService.changeUserStatusById(id);
        System.out.println("更改成功！");
        return "redirect:/employee";
    }


}
